package io.adrainty.algorithm.leetcode.normal;

/**
 * @author adrainty
 * @version v1.0.0
 * @since 2024/12/26 下午3:04
 */
public class Solution75 {

    public void sortColors(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int cur = 0;

        while (cur <= right) {
            if (nums[cur] == 0) {
                swap(nums, left, cur);
                left++;
                cur++;
            } else if (nums[cur] == 2) {
                swap(nums, cur, right);
                right--;
            } else {
                cur++;
            }
        }
    }

    public static void main(String[] args) {
        Solution75 solution = new Solution75();
        solution.sortColors(new int[]{2, 0, 2, 1, 1, 0});
    }

    private void swap(int[] nums, int left, int right) {
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }

}
